package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.domain.AjaxResult;
import com.itheima.common.domain.PageInfo;
import com.itheima.service.ISmsHomeAdvertiseService;
import com.itheima.sms.entity.SmsHomeAdvertise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Map;

@RestController
@RequestMapping("/home/advertise")
public class SmsHomeAdvertiseController {
    @Autowired
    private ISmsHomeAdvertiseService smsHomeAdvertiseService;

    /**
     * 添加广告
     *
     * @param smsHomeAdvertise
     * @return
     */
    @PostMapping("/create")
    public AjaxResult create(@RequestBody SmsHomeAdvertise smsHomeAdvertise) {
        smsHomeAdvertiseService.save(smsHomeAdvertise);
        return AjaxResult.success();
    }

    /**
     * 删除广告
     *
     * @param map
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, Object> map) {
        ArrayList<Long> ids = (ArrayList<Long>) map.get("ids");
        smsHomeAdvertiseService.removeByIds(ids);
        return AjaxResult.success();
    }

    /**
     * 分页查询广告
     *
     * @param endTime
     * @param name
     * @param pageNum
     * @param pageSize
     * @param type
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "endTime", required = false) String endTime,
                           @RequestParam(value = "name", required = false) String name,
                           @RequestParam(value = "pageNum", required = false) Integer pageNum,
                           @RequestParam(value = "pageSize", required = false) Integer pageSize,
                           @RequestParam(value = "type", required = false) Integer type) {

        Page<SmsHomeAdvertise> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeAdvertise> lqw = new LambdaQueryWrapper<>();
        lqw.lt(endTime != null, SmsHomeAdvertise::getEndTime, endTime)
                .eq(name != null, SmsHomeAdvertise::getName, name)
                .eq(type != null, SmsHomeAdvertise::getType, type);

        smsHomeAdvertiseService.page(page, lqw);
        PageInfo pageInfo = PageInfo.toPageList(page);

        return AjaxResult.success(pageInfo);
    }

    /**
     * 不分页查询所有
     * @return
     */
    @GetMapping("listAll")
    public AjaxResult listAll(){
        return AjaxResult.success(smsHomeAdvertiseService.list());
    };


    /**
     * 修改上下线状态
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/update/status/{id}")
    public AjaxResult updateStatus(@PathVariable Integer id, @RequestBody Integer status) {
        LambdaUpdateWrapper<SmsHomeAdvertise> luw = new LambdaUpdateWrapper<>();
        luw.eq(SmsHomeAdvertise::getId, id)
                .set(status != null, SmsHomeAdvertise::getStatus, status);
        smsHomeAdvertiseService.update(luw);

        return AjaxResult.success();
    }

    /**
     * 修改广告
     *
     * @param id
     * @param smsHomeAdvertise
     * @return
     */
    @PostMapping("/update/{id}")
    public AjaxResult updateId(@PathVariable Integer id, @RequestBody SmsHomeAdvertise smsHomeAdvertise) {
        smsHomeAdvertiseService.updateById(smsHomeAdvertise);
        return AjaxResult.success();
    }

    /**
     * 获取广告详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Integer id) {
        SmsHomeAdvertise byId = smsHomeAdvertiseService.getById(id);
        return AjaxResult.success(byId);
    }
}
